﻿using NFine.Domain.Entity.BusinessManage;
using NFine.Domain.IRepository.BusinessManage;
using NFine.Repository.BusinessManage;
using NFine.Code;
using System.Collections.Generic;
using System.Linq;
using NFine.Domain.Entity.SystemManage;
using Newtonsoft.Json;
using NFine.Domain.IRepository.SystemManage;
using NFine.Repository.SystemManage;

namespace NFine.Application.BusinessManage
{
    public class D_AmountChangeApp
    {
        private ID_AmountChangeRepository service = new D_AmountChangeRepository();

        public List<D_AmountChangeEntity> GetList(Pagination pagination, string keyword)
        {
            var userId = OperatorProvider.Provider.GetCurrent().UserId;
            var expression = ExtLinq.True<D_AmountChangeEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.F_No.Contains(keyword));
                //expression = expression.Or(t => t.F_Summary.Contains(keyword));
                //expression = expression.Or(t => t.F_Spec.Contains(keyword));
            }
            expression = expression.And(t => t.F_AccountId.Equals(userId));
            expression = expression.And(t => t.F_ActionType.Equals(1));
            return service.FindList(expression, pagination);
        }

        public List<D_AmountChangeEntity> GetAllList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<D_AmountChangeEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                //expression = expression.And(t => t.F_No.Contains(keyword));
                expression = expression.And(t => t.F_AccountName.Contains(keyword));
                //expression = expression.Or(t => t.F_Spec.Contains(keyword));
            }
            expression = expression.And(t => t.F_ActionType.Equals(1));
            return service.FindList(expression, pagination);
        }

        public List<D_AmountChangeEntity> GetTransList(Pagination pagination, string keyword)
        {
            var userId = OperatorProvider.Provider.GetCurrent().UserId;
            var model = new D_AmountChangeEntity();
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                model = JsonConvert.DeserializeObject<D_AmountChangeEntity>(keyword);
            }
            var expression = ExtLinq.True<D_AmountChangeEntity>();
            var expression1 = ExtLinq.True<UserEntity>();
            if (!string.IsNullOrEmpty(model.F_UserName))
            {
                expression1 = expression1.And(t => t.F_RealName.Contains(model.F_UserName));
            }
            if (!string.IsNullOrEmpty(model.F_UserNo))
            {
                expression1 = expression1.And(t => t.F_Account.Contains(model.F_UserNo));
            }
            expression = expression.And(t => t.F_AccountId.Equals(userId));
            expression = expression.And(t => t.F_ActionType.Equals(0));
            return service.FindOwnTransList(expression, expression1, pagination);
        }

        public List<D_AmountChangeEntity> GetList()
        {
            return service.IQueryable().ToList();
        }

        public List<D_AmountChangeEntity> GetGrid(string keyword)
        {
            var expression = ExtLinq.True<D_AmountChangeEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.F_Id == keyword);
            }
            return service.FindList(expression);
        }
        public D_AmountChangeEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public D_AmountChangeEntity GetMemberForm(string keyValue,string userId)
        {
            return service.FindMemberEntity(keyValue, userId);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void SubmitForm(D_AmountChangeEntity goodEntity, string keyValue)
        {
            var loginid = OperatorProvider.Provider.GetCurrent().UserId;
            IUserRepository userservice = new UserRepository();
            UserEntity LoginUser = userservice.FindEntity(loginid);

            if (!string.IsNullOrEmpty(keyValue))
            {
                service.SubmitForm(goodEntity, LoginUser, keyValue);
            }
            else
            {
                goodEntity.Create();
                service.SubmitForm(goodEntity, LoginUser, keyValue);
            }
        }

        public void ClearInitAmount()
        {
            service.ClearInitAmount();
        }

        public void CheckForm(D_AmountChangeEntity goodEntity, string keyValue)
        {
            goodEntity.Modify(keyValue);
            service.UpdateForm(goodEntity, keyValue);
        }

        public void SubTransfer(D_AmountChangeEntity goodEntity)
        {
            goodEntity.Create();
            service.SubTransfer(goodEntity);
        }
    }
}
